﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.ComponentModel;
using FinPlusInterfaces;
using FinPlusCompCore;
using FinPlusAssembler;

namespace FinPlusCompTradeParsers
{
    public class BBParseIRFutTrade : FinPlusComponent
    {
        private enum Params { ParseTradeAction }
        private readonly IFinPlusComp _instrumentBuildConn, _tradeAdapterConn;
        private readonly FinPlusConnectDB _tradeSaveConn;
        private readonly IPod _tradeAdapter;

        //construct
        public BBParseIRFutTrade(IFinPlusComp tradeAdapters)
        {
            _tradeAdapter = tradeAdapters.Adapter.First("Name='InstrumentType' And Value='IRFut'");
            _tradeAdapter.Get(Params.ParseTradeAction.ToString(), Trade_Parse);
        }
        
        //common control interface
        public override void Dispose()
        {
            try
            {
               //TODO
            }
            catch (Exception e)
            {
                IsValid = Level.Warning.Log(e.Message, e);
            }
        }

        public override void CmdExecute(IPod command)
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.QuoteAccept: ExecutionParse(command); break;
                    case Cmds.QuoteAcceptPublish: ExecutionParse(command); break;
                    default: throw new Exception(string.Format("cmd not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
               IsValid =  Level.Error.Log(Id, Config, e.Message, e);
            }
        }

        //private
        private void ExecutionParse(IPod pod)
        {
            try
            {
                //TODO trade parse
                //TODO save trade
                //lack of bloomberg bridge cant implement at
                //this stage, any static required can be 
                //added to IRFutureBuild 
            }
            catch (Exception e)
            {
               IsValid =  Level.Error.Log(pod.ToString(), e);
            }
        }

        //cmds
        private enum Cmds { QuoteAccept, QuoteAcceptPublish };

        //events
        private void Trade_Parse(object s, PropertyChangedEventArgs a) { CmdExecute((IPod)((IBean)s).Object); }//QuoteAccept && QuotePublishAccept
    }
}
